package com.vcs.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ApplicationAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.TextProvider;
import com.vcs.dao.UtilDAO;
import com.vcs.dao.UtilDAOImpl;
import com.vcs.domain.EmploymentType;
import com.vcs.domain.FunctionalArea;
import com.vcs.domain.Industry;
import com.vcs.domain.Location;
import com.vcs.domain.Qualification;

@SuppressWarnings("serial")
public class MiscAction extends ActionSupport implements TextProvider,
		ApplicationAware {

	Log log = LogFactory.getLog(MiscAction.class);
	private Map<String, Object> applicationMap;
	UtilDAO utilDao = new UtilDAOImpl();

	@Override
	public void setApplication(Map<String, Object> applicationMap) {
		this.applicationMap = applicationMap;
	}

	@Override
	public String execute() {
		// if industry list is not in application scope then set it.

		if (!applicationMap.containsKey("industryList")) {
			log.info("--------> setting industryList in app scope");
			setIndustryListInAppScope();
		}
		if (!applicationMap.containsKey("locationList")) {
			log.info("--------> setting locationList in app scope");
			setLocationListInAppScope();
		}
		if (!applicationMap.containsKey("funcAreaList")) {
			log.info("--------> setting funcAreaList in app scope");
			setFunctionalAreaListInAppScope();
		}
		if (!applicationMap.containsKey("qualificationList")) {
			log.info("--------> setting qualificationList in app scope");
			setQualificationListInAppScope();
		}
		if(!applicationMap.containsKey("compensationAmtList")){
			log.info("--------> setting compensationAmtList in app scope");
			setCompensationAmtListInAppScope();
		}
		if(!applicationMap.containsKey("employmentTypeList")){
			setEmploymentTypeListInAppScope();
		}

		log.info("--------> Lists are set in app scope");
		return SUCCESS;
	}

	
	private void setIndustryListInAppScope() {
		List<Industry> industryList = utilDao.getIndustryList();
		
		applicationMap.put("industryList", industryList);
	}

	private void setLocationListInAppScope() {
		List<Location> locationList = utilDao.getLocationList();
		Collections.sort(locationList);
		applicationMap.put("locationList", locationList);
	}

	private void setFunctionalAreaListInAppScope() {
		List<FunctionalArea> funcArea = utilDao.getFuncationalAreaList();
		Collections.sort(funcArea);
		applicationMap.put("funcAreaList", funcArea);
	}

	private void setQualificationListInAppScope() {
		List<Qualification> qualificationList = utilDao.getQualificationList();
		Collections.sort(qualificationList);
		applicationMap.put("qualificationList", qualificationList);
	}
	
	private void setCompensationAmtListInAppScope(){
		List compensationAmtList = new ArrayList();
		for(int i = 25000; i <= 5000000; i+= 25000){
			compensationAmtList.add(i);
		}
		log.info("compensationAmtList: "+ compensationAmtList);
		applicationMap.put("compensationAmtList", compensationAmtList);
	}
	
	private void setEmploymentTypeListInAppScope(){

		List<EmploymentType> employmentType = new ArrayList<EmploymentType>();
		employmentType.add(new EmploymentType(1,"Permanent"));
		employmentType.add(new EmploymentType(2,"Temporary/Contractual"));
		employmentType.add(new EmploymentType(3,"Freelancer"));
		
		applicationMap.put("employmentTypeList", employmentType);
	}
	
	/**
	 * Method to get Stickey note content.
	 *  
	 */
	public String getStickeyNoteContent(){
		// Call dao method to get stickey note content from database
		
		return SUCCESS;
	}

}
